import numpy as np
import torch
import random
import math
import torch.nn.functional as F


t4d = torch.ones(3, 3, 4, 2)
print('t4d\n', t4d)
p1d = (1, 1) # pad last dim by 1 on each side 即1+1=2添加到最后一维
out = F.pad(t4d, p1d, "constant", 0)  # effectively zero padding
print(out.size())
# print('out\n', out)
torch.Size([3, 3, 4, 4])
p2d = (1, 1, 2, 2) # pad last dim by (1, 1) and 2nd to last by (2, 2) 1+1=2添加到最后一维，2+2=4添加到倒数第二维
out = F.pad(t4d, p2d, "constant", 0)
print('out\n', out)
print(out.size())
torch.Size([3, 3, 8, 4])
t4d = torch.empty(3, 3, 4, 2)
p3d = (0, 1, 2, 1, 3, 3) # pad by (0, 1), (2, 1), and (3, 3) 3+3=6添加到倒数第三维
out = F.pad(t4d, p3d, "constant", 0)
print(out.size())
torch.Size([3, 9, 7, 3])




